package com.example.library_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.library_demo.entity.Card;
import com.example.library_demo.exception.BizException;
import com.example.library_demo.mapper.CardMapper;
import com.example.library_demo.service.ICardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.library_demo.vo.card.CardVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Eric
 * @since 2024-11-28
 */
@Service
public class CardServiceImpl extends ServiceImpl<CardMapper, Card> implements ICardService {

    @Override
    public Boolean checkIsExistByCardNum(String cardNum) {
        LambdaQueryWrapper<Card> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Card::getCardNum,cardNum);
        return count(wrapper)>0;
    }

    @Override
    public CardVO getCardByCardNumber(String cardNum) {
        LambdaQueryWrapper<Card> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Card::getCardNum,cardNum);
        Card card = getOne(wrapper);
        if(null == card){
            return null;
        }
        CardVO vo = new CardVO();
        BeanUtils.copyProperties(card,vo);
        return vo;
    }

    @Override
    public List<CardVO> getCardByUserId(Long userId) {
        LambdaQueryWrapper<Card> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Card::getUserId,userId);
        List<Card> list = list(wrapper);
        List<CardVO> rst = new ArrayList<>();
        list.forEach(x->{
            CardVO vo = new CardVO();
            BeanUtils.copyProperties(x,vo);
            rst.add(vo);
        });
        return rst;
    }
}
